﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using LIM.controller;
using LIM.model;
using Telerik.WinControls.UI;

namespace LIM.forms
{
    public partial class radForm_CRM_DanhSachBaoGia : Telerik.WinControls.UI.RadForm
    {
        public radForm_CRM_DanhSachBaoGia()
        {
            InitializeComponent();
            this.radTreeView_BaoGia.TreeViewElement.AllowAlternatingRowColor = true;
        }
        DanhMucBaoGiaController controller = new DanhMucBaoGiaController();
        private void radForm_CRM_DanhSachBaoGia_Load(object sender, EventArgs e)
        {
            LoadDM_HeThong();
            radGridView2.DataSource = null;
        }
        private void LoadDM_HeThong()
        {
            radTreeView_BaoGia.Nodes.Clear();
            foreach (tbl_DanhMuc_HeThong item in controller.LoadDataList("TinhTrangBaoGia"))
            {
                RadTreeNode node1 = new RadTreeNode(item.TenGoi);
                node1.Tag = item.MaDanhMuc;
                node1.Font = new Font("Segoe UI", 825/100, FontStyle.Bold);
                radTreeView_BaoGia.Nodes.Add(LoadDM_Child(item.RowID, node1));
                radTreeView_BaoGia.ExpandAll();
            }
        }
        private RadTreeNode LoadDM_Child(int parenID, RadTreeNode trn)
        {
            foreach (tbl_DanhMuc_HeThong itemcon in controller.LoadDataChild(parenID))
            {
                RadTreeNode trcon = new RadTreeNode(itemcon.TenGoi);
                trcon.Tag = itemcon.MaDanhMuc;
                trcon.Text = itemcon.TenGoi + " (" + controller.LoadBaoGia(true, itemcon.MaDanhMuc, false, 0).Count.ToString() + ")";
                trn.Nodes.Add(trcon);
                if (controller.LoadDataChild(itemcon.RowID).Count > 0)
                {
                    LoadDM_Child(itemcon.RowID, trcon);
                }

                //tbl_DanhMuc_HeThong dmht = controller.LoadDataByRowID(itemcon.RowID);
                //if (dmht != null)
                //{
                //    if (dmht.TenGoi != "Hủy bỏ")
                //    {
                //        RadTreeNode trcon1 = new RadTreeNode((DateTime.Now.Year).ToString());
                //        trcon1.Tag = (DateTime.Now.Year).ToString();
                //        trcon.Nodes.Add(trcon1);
                //        RadTreeNode trcon2 = new RadTreeNode((DateTime.Now.Year - 1).ToString());
                //        trcon2.Tag = (DateTime.Now.Year - 1).ToString();
                //        trcon.Nodes.Add(trcon2);
                //        RadTreeNode trcon3 = new RadTreeNode("Older");
                //        trcon3.Tag = (DateTime.Now.Year - 1).ToString();
                //        trcon.Nodes.Add(trcon3);
                //    }
                //}
            }

            return trn;
        }

        private void radTreeView_BaoGia_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
        {
            LoadSelectedNodeChanged(e);
        }
        void LoadSelectedNodeChanged(Telerik.WinControls.UI.RadTreeViewEventArgs e)
        {
            if (e != null)
            {
                if (e.Node.Level == 0)
                {
                        radGridView2.DataSource = controller.LoadBaoGia(null, e.Node.Text, false, 0);
                }
                else if (e.Node.Level == 1)
                {
                    radGridView2.DataSource = controller.LoadBaoGia(true, e.Node.Tag.ToString(), false, 0);

                }
                else if (e.Node.Level == 2)
                {
                    int RowID = 0;
                    if (e.Node.Text.Contains("Older"))
                    { RowID = DateTime.Now.Year; }
                    else
                    {
                        RowID = int.Parse(e.Node.Tag.ToString());
                    }
                    if (e.Node.Text.Contains("Older"))
                    {
                        radGridView2.DataSource = controller.LoadBaoGia(false, e.Node.Parent.Tag.ToString(), true, RowID);
                    }
                    else
                    {
                        radGridView2.DataSource = controller.LoadBaoGia(false, e.Node.Parent.Tag.ToString(), false, RowID);
                    }
                }
            }
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachBaoGia_Load(sender, e);
        }

        private void radGridView2_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            if (radGridView2.SelectedRows.Count > 0)
            {
                if (radGridView2.SelectedRows[0].Cells[0].Value != null)
                {
                    radForm_CRM_BaoGia_ChiTiet frm = new radForm_CRM_BaoGia_ChiTiet();
                    int iBaoGiaID = int.Parse(radGridView2.SelectedRows[0].Cells[0].Value.ToString());
                    frm.Load += (obj, ev) =>
                        {
                            frm.SetControlValues(iBaoGiaID);
                        };
                    frm.MdiParent = this.MdiParent;
                    frm.Show();
                }
            }
        }
    }
}
